﻿package com.wabbei.friendcenter.action;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.webbei.friendcenter.service.MyFriendCenter;
import com.webbei.pojo.Messages;

public class SendMessageEachOtherByAjAxAction extends ActionSupport  {
	private static final long serialVersionUID = 1L;
	private MyFriendCenter friservice;
	private String messageContent;
	private String fromName;
	private String toName;
	private Date date;
	private Messages message;
	private String uname;
	private File uploadify;
	private String uploadifyFileName;
	//private static int s=0; 
	private InputStream inputstream;
	
	
	public InputStream getInputstream() {
		//System.out.println(uploadifyFileName[0]+"wqwqwq");
		//System.out.println(uploadify.length+",length");
		String result="true";
		date=new Date();
		fromName=(String)ServletActionContext.getContext().getSession().get("username");
		message=new Messages();
		message.setFromName(fromName);
		message.setToName(toName);
		message.setMessageTime(date);
		
		//message.setMessageContent(messageContent);
		try {
			messageContent=URLDecoder.decode(messageContent,"utf-8");
			messageContent=URLDecoder.decode(messageContent,"utf-8");
			message.setMessageContent(messageContent);
			//message.setMessageContent(new String(messageContent.getBytes("utf8"),"utf8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//message.setMessagePic("");
		//message.setCDRfilePath("");
		System.out.println("toName:"+toName+",content:"+messageContent);
		/*if(uploadify==null){
			message.setMessagePic("");
			message.setCDRfilePath("");
		}else{
			if(uploadify.length==1){
				message.setMessagePic(uploadifyFileName[0]);
			}else if(uploadify.length==2){
				message.setMessagePic(uploadifyFileName[0]);
				message.setCDRfilePath(uploadifyFileName[1]);
			}
		}*/
		if(uploadify!=null){
			message.setMessagePic(uploadifyFileName);
		}else{
			message.setMessagePic(null);
		}
		
		
			int i=friservice.insertMessageEachOther(message);
			if(i>0){
				result="true";
			}else{
				result="false";
			}
			System.out.println("i:"+i);
		
		    inputstream=new ByteArrayInputStream(result.getBytes());
		    if(uploadify!=null){
		    	copyFile(uploadify,uploadifyFileName);
		    }
		    return inputstream;
	}
	public void setInputstream(InputStream inputstream) {
		this.inputstream = inputstream;
	}
	public MyFriendCenter getFriservice() {
		return friservice;
	}
	public void setFriservice(MyFriendCenter friservice) {
		this.friservice = friservice;
	}
	public String getMessageContent() {
		return messageContent;
	}
	public void setMessageContent(String messageContent) {
		this.messageContent = messageContent;
	}
	public String getFromName() {
		return fromName;
	}
	public void setFromName(String fromName) {
		this.fromName = fromName;
	}
	public String getToName() {
		return toName;
	}
	public void setToName(String toName) {
		this.toName = toName;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public Messages getMessage() {
		return message;
	}
	public void setMessage(Messages message) {
		this.message = message;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public File getUploadify() {
		return uploadify;
	}
	public void setUploadify(File uploadify) {
		this.uploadify = uploadify;
	}
	public String getUploadifyFileName() {
		return uploadifyFileName;
	}
	public void setUploadifyFileName(String uploadifyFileName) {
		this.uploadifyFileName = uploadifyFileName;
	}
	public String check(){
		/*HttpServletResponse response = ServletActionContext.getResponse();
	    response.setCharacterEncoding("utf-8"); //务必，防止返回文件名是乱码 
*/		
		return "success";
	}
	
	public boolean copyFile(File f,String fName){
		boolean b=true;
		if(f!=null){
			//System.out.println("imageandfilelength:"+imageandfile.length);
			/*for(String s:imageandfileFileName){
				System.out.println(s);
			}*/
				String realPath=ServletActionContext.getServletContext().getRealPath("/"+toName);
				System.out.println(realPath);
				File newFile=new File(realPath);
				if(!newFile.exists()){
					newFile.mkdirs();
				}
				try {
					FileUtils.copyFile(f,new File(newFile,fName));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("send file successful");
		}else{
			b=false;
		}
		return b;
	}
}
